using System;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace Liberate.VSIntergration
{
    public static class OutputWindow
    {
        private static readonly Guid libernatePaneGuid = new Guid("EF914AB4-A08D-4291-AFBA-6025C21EFD9C");
        private static OutputWindowPane libernatePane;

        public static void ShowOutputWindow()
        {
            DTE dte = DTEHelper.GetDTEInstance();
            Window win = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
            if(win != null)
            {   
                win.Visible = true;
                win.Activate();
                win.AutoHides = false;
            }
        }

        public static OutputWindowPane LibernatePane
        {
            get
            {
                if (libernatePane == null)
                {
                    libernatePane = GetPane(libernatePaneGuid);
                    libernatePane.Name = "libernate";
                }
                return libernatePane;
            }
        }

        public static OutputWindowPane GetPane(Guid guid)
        {
            IVsOutputWindowPane pane;
            int getSuccess = GetWindowPane(guid, out pane);
            if (getSuccess != VSConstants.S_OK || pane == null)
            {
                CreateWindowPane(guid, null);
                GetWindowPane(guid, out pane);
            }
            return new OutputWindowPane(pane);

        }
        public static int CreateWindowPane(Guid guid, string name)
        {
            return OutputWindowInstance.CreatePane(
              ref guid,
              name ?? "liberate",
              0,
              0);
        }
        private static int GetWindowPane(Guid guid, out IVsOutputWindowPane pane)
        {
            return OutputWindowInstance.GetPane(ref guid, out pane);
        }

        private static IVsOutputWindow OutputWindowInstance
        {
            get
            {
                return Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;
            }
        }

        public class OutputWindowPane
        {
            private readonly IVsOutputWindowPane pane;

            public OutputWindowPane(IVsOutputWindowPane pane)
            {
                this.pane = pane;
            }

            public string Name
            {
                set
                {
                    pane.SetName(value);
                }
            }

            public void Activate()
            {
                pane.Activate();
            }

            public void Hide()
            {
                pane.Hide();
            }

            public void WriteLine(string format, params object[] parameters)
            {
                OutputString(string.Format(format, parameters) + "\n");
            }

            public void Clear()
            {
                pane.Clear();
            }

            private void OutputString(string output)
            {
                pane.OutputString(output);
            }

        }
    }
}